import { SectionHeader } from "./SectionHeader.ts";

/**
 * @author tongxin
 * @date 2024/4/20 17:25
 */
export class SectionHeaders {
  static TEXT = ".text" as const;
  static DATA = ".data" as const;
  static BSS = ".bss" as const;
  static RDATA = ".rdata" as const;
  static IDATA = ".idata" as const;
  static TLS = ".tls" as const;
  static PDATA = ".pdata" as const;
  static EXTRA = ".extra" as const;
  static RSRC = ".rsrc" as const;
  dataReader: IPEFileReader;
  offset: number = 0;
  sectionHeaders: Record<string, SectionHeader> = {};

  constructor(dataReader: IPEFileReader) {
    this.dataReader = dataReader;
    this.offset=dataReader.getOffset();

  }

  async read(count: number) {
    this.dataReader.setOffset(this.offset)
    console.log("SectionHeaders read :" + this.dataReader.getOffset().toString(16));
    for (let i = 0; i < count; i++) {
      console.log("SectionHeaders item read :" + this.dataReader.getOffset().toString(16));
      const byteArray: IByteArray = await this.dataReader.read(40);
      const header: SectionHeader = new SectionHeader(byteArray);
      this.sectionHeaders[header.name] = header;
    }
  }

  get(name: string): SectionHeader {
    return this.sectionHeaders[name];
  }

}
